import java.util.*;

/**
 * @ClassName LC30
 * @Author ykx
 * @Date 2024/3/6 17:26
 * @Version 1.0
 */
public class LC30 {
    public static void main(String[] args) {
        LC30 lc30 = new LC30();
        String s = "barfoothefoobarman";
        String[] words = new String[]{"foo", "bar"};
        System.out.println(lc30.findSubstring(s, words));
    }

    public List<Integer> findSubstring(String s, String[] words) {
        // 结果
        List<Integer> result = new ArrayList<>();
        List<String> wordList = Arrays.asList(words);
        Collections.sort(wordList);
        int n = s.length();
        int w = words.length;
        int m = words[0].length();
        for (int i = 0; i < n - m * w + 1; i++) {
            String sub = s.substring(i, m * w + i);
            List<String> tmpList = new ArrayList<>();
            for (int j = 0; j < sub.length(); j = j + m) {
                tmpList.add(sub.substring(j, j + m));
            }
            Collections.sort(tmpList);
            if (Objects.equals(wordList, tmpList)) {
                result.add(i);
            }
        }
        return result;
    }


}
